# encoding: utf-8
import json

from django.http import HttpResponse
from django.shortcuts import render
from packages.Controller import Controller

controller = Controller()


def welcome(request):
    return render(request, "index.html")


def signup(request):
    return render(request, "signup.html")


def query(request):
    return render(request, "query.html")


def transfor(request):
    return render(request, "transfor.html")


def deposit(request):
    return render(request, "deposit.html")


def changepwd(request):
    return render(request, "changepwd.html")


# 提交注册表单
def signup_action(request):
    username = request.POST.get('username')
    phonenumber = request.POST.get('phonenumber')
    id_number = request.POST.get('id_number')
    password = request.POST.get('password')
    # 执行controller对象中的注册方法，获取返回的卡号
    data = controller.register(username, id_number, password)
    # 将数据以json格式返回
    return HttpResponse(json.dumps(data, ensure_ascii=False), content_type="application/json")


# 提交查询表单
def query_action(request):
    card_number = request.POST.get('card_number')
    password = request.POST.get('password')
    # 执行controller对象中的查询方法，获取余额
    data = controller.query(card_number, password)
    # 将数据以json格式返回
    return HttpResponse(json.dumps(data, ensure_ascii=False), content_type="application/json")


# 提交转账表单
def transfor_action(request):
    card_number = request.POST.get('card_number')
    password = request.POST.get('password')
    transfor_card_number = request.POST.get('transfor_card_number')
    transfor_money = request.POST.get('transfor_money')
    # 执行controller对象中的转账方法
    data = controller.transfor_money(card_number, password, transfor_card_number, transfor_money)
    # 将数据以json格式返回
    return HttpResponse(json.dumps(data, ensure_ascii=False), content_type="application/json")


# 提交存款表单
def deposit_action(request):
    card_number = request.POST.get('card_number')
    password = request.POST.get('password')
    deposit_money = request.POST.get('deposit_money')
    # 执行controller对象中的存款方法
    data = controller.add_money(card_number, password, deposit_money)
    # 将数据以json格式返回
    return HttpResponse(json.dumps(data, ensure_ascii=False), content_type="application/json")


# 提交改密表单
def changepwd_action(request):
    card_number = request.POST.get('card_number')
    old_password = request.POST.get('old_password')
    id_number = request.POST.get('id_number')
    new_password = request.POST.get('new_password')
    # 执行controller对象中的改密方法
    data = controller.change_pwd(card_number, old_password, id_number, new_password)
    # 将数据以json格式返回
    return HttpResponse(json.dumps(data, ensure_ascii=False), content_type="application/json")
